﻿Ext.define('app.prm.product.ProductController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.product',
    init: function () {
        var me = this;
    },
    onAdd: function () {
        var main = this;
        var win = Ext.create("app.prm.project.EditProduct");
        win.on("savesuccess", function () {
            main.getView().store.reload();
            win.close();
        });
        win.show();
    },
    onEdit: function () {
        var main = this;
        var grid = main.getView();
        var selections = grid.getSelection();
        if (selections.length == 1) {
            var win = Ext.create("app.prm.project.EditProduct", { productId: selections[0].get('id'), subId: selections[0].get('substanceId') });
            win.on("savesuccess", function () {
                main.getView().store.reload();
                win.close();
            });
            win.show();
        } else {

            Ext.Msg.alert('系统提示', '请选择一条记录来编辑');
        }

    },
    onDel: function () {
        var main = this;
        var grid = main.getView();
        var selections = grid.getSelection();
        if (selections.length == 1) {
            Ext.Msg.confirm("警告", "确定删除该产品么？", function (btn) {
                if (btn == 'yes') {

                    var record = selections[0];
                    var productId = record.get('id');
                    App.AjaxDelete('/api/services/app/substance/RemoveProduct?productId=' + productId, function (data) {
                        console.log(data);
                        main.onRefresh();
                    });
                }
            });
        } else {
            Ext.Msg.alert('系统提示', '请选择一个要删除得产品');
        }
    },
    onRefresh: function () {
        var main = this;
        var grid = main.getView();
        grid.getStore().reload();
    },
    onOpenDetail: function (grid, rowIndex, colIndex) {
        var record = grid.store.getAt(rowIndex);
        Ext.util.Cookies.set('productinfo' + record.data.id, record.data.code);
        this.redirectTo('app.prm.product.Detail/' + record.data.id);
    },
    onProductRowDbClick:function(grid, record, tr, rowIndex, e, ept) {
        var record = grid.store.getAt(rowIndex);
        Ext.util.Cookies.set('productinfo' + record.data.id, record.data.code);
        this.redirectTo('app.prm.product.Detail/' + record.data.id);
    },
    onDbClick: function (grid, record, tr, rowIndex, e, ept) {
        //Ext.util.Cookies.set('projectinfo' + record.data.id, record.data.projectName + "(" + record.data.projectNo + ")");

        //this.redirectTo('app.prm.detail.Index/' + record.data.id);

        var column = e.position.column;
        var value = record.data[column.dataIndex];
        console.log(value);
        if (value == undefined || value.indexOf('/') == -1) {

            Ext.util.Cookies.set('projectinfo' + record.data.projectId, record.data.projectName + "(" + record.data.projectNo + ")");

            this.redirectTo('app.prm.detail.Index/' + record.data.projectId);
            return;
        }
        var v = value.split('/')[0];
        if (v == 0) {
            return false;
        }

        var DocTypeId = record.data.DocTypeId;
        var projectId = 0;
        var projectName = '';
        var projectNo = '';
        var docTypeId = 0;

        if (DocTypeId >= 0) {
            if (DocTypeId == 0) {
                return false;
            }
            projectId = column.projectId;
            projectName = column.text;
            projectNo = column.projectNo;
            docTypeId = DocTypeId;
        }
        else {
            projectId = record.data.projectId;
            projectName = record.data.projectName;
            projectNo = record.data.projectNo;
            docTypeId = column.docType;
        }
        Ext.util.Cookies.set('projectdocument' + projectId, projectName + "(" + projectNo + ")文档");
        Ext.util.Cookies.set('projectdocumentreport' + projectId, docTypeId);
        this.redirectTo('app.prm.document.Index/' + projectId);
    },
    onCellClick: function (table, td, cellIndex, record, tr, rowIndex, e, eOpts) {
        var t = e.position.column.dataIndex;
        if (t == 'projectNo') {
            Ext.util.Cookies.set('projectinfo' + record.data.projectId, record.data.projectName + "(" + record.data.projectNo + ")");

            this.redirectTo('app.prm.detail.Index/' + record.data.projectId);
        }
        else if (t == 'productNo') {
            if (record.data.productId != 0) {
                this.redirectTo('app.prm.product.Detail/' + record.data.productId);
            }
        }
    },
    onOpenDocument: function (table, td, cellIndex, record, tr, rowIndex, e, eOpts) {
        var column = e.position.column;
        var value = record.data[column.dataIndex];

        if (value == undefined || value.indexOf('/') == -1) {
            return false;
        }
        var v = value.split('/')[0];
        if (v == 0) {
            return false;
        }

        Ext.util.Cookies.set('projectdocument' + record.data.projectId, record.data.projectName + "(" + record.data.projectNo + ")文档");
        Ext.util.Cookies.set('projectdocumentreport' + record.data.projectId, column.docTypeId);
        this.redirectTo('app.prm.document.Index/' + record.data.projectId);
    }
});
